<?php
class FlightController extends AppController{
    var $name = 'Flight';
	//使用User,Flight,Plane,BookInfo,Quene这五个Modul
	var $uses = array('User','Flight','Plane','BookInfo','Quene');
    
    //beforeFilter 页面执行前执行
	//afterFilter 页面执行后执行
	//beforeRender 页面显示前执行
	//afterRender 页面显示后执
    function beforeFilter(){
		Configure::write('debug' , 1);//关闭调试
	}
	
	function index(){}
	
	//获取航班 输入项：出发地点，终点，时间(  日期 ) 返回项：航班列表
    function getResult( $start, $end, $time ){
		
        $flights = $this->Flight->find('all',array('conditions' => array('Filght.start'=> $start,'Flight.end'=> $end,'Flight.time' => $time)));
		return $flights;
		
	}
	
	//获取指定航班信息  输入项：航班ID号  返回项：航班信息列表，航班所属飞机信息列表
	function getFlightInfo( $id ){
	
		$flight = $this->Flight->find('all', 
			array('conditions' => array('Flight.id' => $id)));
        return $flight;
	}
	
	//订票  输入项：航班ID号，用户ID号，座位列表，订票数量  返回项：订票成功或者失败原因
	function orderTickets( $id, $userId, $seat, $quantity ){
		
		$bookInfo = array('BookInfo'=>
						array('flight'=>$id,
							  'user'=>$userId,
							  'seat'=>$seat,
							  'quantity'=>$quantity
							  )
						);
       if( $this->BookInfo->save($bookInfo)){
		   return true;
	   }
	   else return false;
		
	}
	
	//退票  必须项：订票ID号  可选项：部分退票的座位号和数量  返回项：退订成功或者失败原因
	function unsubTickets( $id, $seat = NULL, $quantity = NULL ){
		  if(!empty($id)){ 
		     $order = $this->BookInfo->find('all', array('conditions' => array('BookInfo.id' => $id)));
			 $flight_id = $order['BookInfo']['flight'];
			 $flight = $this->Flight->find('all', array('conditions' => array('Flight.id' => $flight_id)));
			 $booked = $flight['Flight']['booked'] - $quantity;
			 $flight['Flight']['booked'] = $booked;
			 
			 $this->Flight->save($flight);
   		     $this->BookInfo->id = $id;
		     $this->BookInfo->del();
			 return true;
		  }
		  else{
			 return false;
		  }
		
	}
	
	//通知排队人  输入项：用户id号，通知信息  返回项：通知成功或者失败原因
	function notifyNext( $id, $message ){
	
	}
	
	//排队  输入项：航班ID号，用户ID号，订票数量  存入数据库时应该加上“排队时  返回项：排队成功或者失败原因间”  
	function addQuene( $id, $userId, $quantity ){
	  
	       	$queue = array('Queue'=>
						   array('flight'=>$id,
							     'user'=>$userId,
							     'quantity'=>$quantity
							  )
						);
			if( $this->BookInfo->save($bookInfo)){
		        return true;
	        }
	        else 
				return false;	   
	}
    
}
?>